function embed(cmd, mode, indir, outdir, rate, param1)
% EMBED  embed message into images.
% EMBED(CMD, MODE, INDIR, OUTDIR, RATE[, PARAM1]) embeds message
% into images in INDIR and store results in OUTDIR. OUTDIR must be
% previously created. RATE is count in bpc, use 1.0 or greater to embed
% into all bits.
%
% Where CMD is one of the following string:
%   'jsteg'   : uses 'JSTEG' to hide message into every image.
%   'f3'      : uses 'F3' to hide message into every image.
%   'f4'      : uses 'F4' to hide message into every image.
%   'f5'      : uses 'F5' to hide message into every image, where
%               PARAM1 specifies the 'K' in F5, and its default value
%               is 2.
%
% MODE is one of the following integer value:
%   0     : const value loaded from 'msg.txt' => embed in sequence.
%   1     : random generated (full) => embed in sequence.
%   2     : random generated (rate in bpp) => embed in random sequence.
%   3     : random generated (rate in bpc) => embed in random sequence.
%
% AUTOCOMPLETION:
%   Run "edit(fullfile(matlabroot,'toolbox/local/TC.xml'))" and append following lines:
%
%   <binding name="embed">
%     <arg argn="1" ctype="VAR" value="jsteg f3 f4 f5"/>
%     <arg argn="3:4" ctype="DIR"/>
%   </binding>

if nargin < 5
    error('embed:TooFewArg', 'Too few input arguments.');
end

p = path();
path(p, ['../codec', pathsep, '../utils', pathsep]);
if mode == 0
    % read message
    fmsg = fopen('msg.txt','r');
    msg = fscanf(fmsg,'%s');
    fclose(fmsg);
end

inlist = dir([indir,'/*.jpg']);
i1 = size(inlist, 1);

if (~isdir(outdir)) && (~mkdir(outdir))
    error('embed:OutputFail','Cannot create directory: %s.',outdir);
end

% open stat file
statname = [outdir,'/stats.txt'];
fstat = fopen(statname, 'w');
if (fstat == -1)
    error('embed:OutputFail', 'Cannot create/write stats file: %s.', statname);
else
    fprintf(1, 'output STATS file is %s.\n',statname);
end
fprintf(fstat,'#pixel\tavail\tembed\tstart\tpath\n');
total_px = 0;
total_eb = 0;
total_av = 0;
count = 0;
for i = 1:i1
    if (inlist(i).isdir == 0) && (inlist(i).bytes > 0)
        % load image
        name = inlist(i).name;
        inname = [indir,'/',name];
        outname = [outdir,'/',name];
        fprintf(1, '%s -> %s\n',inname, outname);
        img = jpeg_read(inname);
        
        if strcmpi(cmd, 'jsteg')
            av = countNonZeroOne(img.coef_arrays{1});
        else
            av = countNonZeroAC2(img.coef_arrays{1});
        end
        
        % generate msg
        px = double(img.image_width) * double(img.image_height);
        switch mode
            case 0
                %pass
            case 1
                msglen = floor(av / 8) - 2;
                if msglen < 0
                    msglen = 0;
                end
                msg = generateArray(msglen);
            case 2
                msglen = floor(rate * px / 8) - 2;
                if msglen < 0
                    msglen = 0;
                end
                msg = generateArray(msglen);
            case 3
                msglen = floor(rate * av / 8) - 2;
                if msglen < 0
                    msglen = 0;
                end
                msg = generateArray(msglen);
            otherwise
                error('embed:UnknownMode', 'Unknown value of mode: %d.', mode);
        end
        
        if mode ~= 0
        end
        
        switch mode
            case {0,1}
                start = -1;
                switch lower(cmd)
                    case 'jsteg'
                        [ret, eb, avail] = jstege_simple(img.coef_arrays{1}, msg);
                    case 'f3'
                        [ret, eb, avail] = f3e_simple(img.coef_arrays{1}, msg);
                    case 'f4'
                        [ret, eb, avail] = f4e_simple(img.coef_arrays{1}, msg);
                    case 'f5'
                        if nargin < 6
                            param1 = 2;
                        end
                        [ret, eb, avail] = f5e_simple(img.coef_arrays{1}, param1, msg);
                end
            case {2,3}
                start = floor(rand()*px);
                switch lower(cmd)
                    case 'jsteg'
                        [ret, eb, avail] = jstege(img.coef_arrays{1}, msg, start);
                    case 'f3'
                        [ret, eb, avail] = f3e(img.coef_arrays{1}, msg, start);
                    case 'f4'
                        [ret, eb, avail] = f4e(img.coef_arrays{1}, msg, start);
                    case 'f5'
                        if nargin < 6
                            param1 = 2;
                        end
                        [ret, eb, avail] = f5e_fix(img.coef_arrays{1}, param1, msg, start);
                end
        end
        img.coef_arrays{1} = ret;
        jpeg_write(img, outname);
        
        fprintf(fstat,'%g\t%g\t%g\t%g\t%s\n',px,avail,eb,start,outname);
        total_px = total_px + px;
        total_eb = total_eb + eb;
        total_av = total_av + av;
        count = count + 1;
        % break % for test only
    end
end

if count ~= 0
    fprintf(fstat,'\naverage value:\n');
    fprintf(fstat,'pixel=%g b\navail=%g b\nembed=%g b',total_px/count,total_av/count,total_eb/count);
end
fclose(fstat);
path(p);

end
